package cn.tedu.csmall.order.webapi.controller;

import cn.tedu.csmall.commons.pojo.order.dto.OrderAddDTO;
import cn.tedu.csmall.commons.pojo.order.entity.Order;
import cn.tedu.csmall.commons.restful.JsonResult;
import cn.tedu.csmall.commons.restful.ResponseCode;
import cn.tedu.csmall.order.service.IOrderService;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/base/order")
@Api("订单模块")
public class OrderController {

    @Autowired
    IOrderService orderService;

    @PostMapping("/add")
    @ApiOperation("添加订单")
    @SentinelResource(value = "新增订单方法",
            blockHandler = "blockError",
            fallback = "fallbackError"
    )
    public JsonResult orderAdd(OrderAddDTO orderAddDTO) {
        orderService.orderAdd(orderAddDTO);
        return JsonResult.ok("添加订单信息成功!");
    }

    public JsonResult blockError(OrderAddDTO orderAddDTO, BlockException e){
        return JsonResult.failed(ResponseCode.INTERNAL_SERVER_ERROR,"请求超过最大请求数量！");
    }

    public JsonResult fallbackError(OrderAddDTO orderAddDTO,Throwable e){
        e.printStackTrace();
        return JsonResult.failed(ResponseCode.INTERNAL_SERVER_ERROR,
                "服务器出发了降级方法!"+e.getMessage());
    }

}
